package com.azusa.xbjy.service.impl;

import com.azusa.xbjy.dao.UserDao;
import com.azusa.xbjy.dao.UserMapper;
import com.azusa.xbjy.entity.User;
import com.azusa.xbjy.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUsernameAndPassword(String username, String password) {
        return userDao.findByUsernameAndPassword(username, password);
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public User save(User user) {
        return userDao.save(user);
    }

    @Override
    public User findUserLookById(int id) {
        User user = userDao.findById(id);
        //查询其关注数
        user.setFocusCount(userMapper.selectUserFocusCountById(id));
        //查询其粉丝数
        user.setFocus(userMapper.selectUserFocus(id));
        return user;
    }

    @Override
    public PageInfo<User> findAllPage(int pageNum, int pageSize, String keyWords,int userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.findByUserName(keyWords);
        List<Integer> focused = userMapper.findFocusedByUserId(userId);
        for (int i = 0; i < userList.size(); i++) {
            userList.get(i).setPassword(null);
            for (int j = 0; j < focused.size(); j++) {
                if (userList.get(i).getId()==focused.get(j)){
                    userList.get(i).setFocused(true);
                }
            }
        }
        return new PageInfo<User>(userList);
    }

    @Override
    public void changeFocused(Integer id, Integer focusUserId, Boolean focus) {
        //关注操作
        if (focus){
            userMapper.addUserFocus(id,focusUserId);
        }else {
            //取关操作
            userMapper.deleteUserfocus(id,focusUserId);
        }
    }

    @Override
    public void updateLook(int id) {
        userMapper.updateLook(id);
    }

    @Override
    public PageInfo<User> findMyUser(int pageNum, int pageSize,int id) {
        //查询关注id集合
        List<Integer> focusedList = userMapper.findFocusedByUserId(id);
        PageHelper.startPage(pageNum, pageSize);
        //查询具体User信息
        List<User> userList = userMapper.findMyUsers(focusedList);
        for (User user : userList) {
            user.setFocused(true);
            user.setPassword(null);
        }
        return new PageInfo<User>(userList);
    }

    @Override
    public List<User> findByDeptId(int deptId) {
        return userDao.findByDeptId(deptId);
    }

    @Override
    public User findByWxOpenid(String openid) {
        return userDao.findByWxOpenid(openid);
    }

    @Override
    public void updateLoginTime(Integer id) {
        userMapper.updateLoginTime(id);
    }
}
